<?php
require_once '../sub/init.php';
header("Cache-control: private");
require_once 'chkUadm.php';
if(!in_array('excel_out',$QXARR))exit(noauth());
if($submitok=="import"){
	require_once ZEAI.'sub/excel_reader.php';
	$file = $_FILES['execl'];
	$file_name = $file['tmp_name'];
	$data = new Spreadsheet_Excel_Reader();
	$data->setOutputEncoding('UTF-8');
	$data->read(iconv("UTF-8","GB2312",$file_name));
	//$data->sheets[0]['numRows']为Excel行数
	//$data->sheets[0]['numCols']为Excel列数
	$echo = '';
	$total=$data->sheets[0]['numRows'];
	$n=0;$s=0;
	for($i=2;$i<=$total;$i++){
		$mob      = trimhtml(dataIO($data->sheets[0]['cells'][$i][1],'in',11));
		$nickname = trimhtml(dataIO($data->sheets[0]['cells'][$i][2],'in',50));
		$sex      = trimhtml(dataIO($data->sheets[0]['cells'][$i][3],'in',2));
		$birthday = trimhtml(dataIO($data->sheets[0]['cells'][$i][4],'in',20));
		$heigh = trimhtml(dataIO($data->sheets[0]['cells'][$i][5],'in',20));
		$weigh = trimhtml(dataIO($data->sheets[0]['cells'][$i][6],'in',20));
		$love  = trimhtml(dataIO($data->sheets[0]['cells'][$i][7],'in',20));
		$edu   = trimhtml(dataIO($data->sheets[0]['cells'][$i][8],'in',100));
		$areatitle = trimhtml(dataIO($data->sheets[0]['cells'][$i][9],'in',100));
		$weixin = trimhtml(dataIO($data->sheets[0]['cells'][$i][10],'in',20));
		$identitynum = trimhtml(dataIO($data->sheets[0]['cells'][$i][11],'in',18));
		$truename = trimhtml(dataIO($data->sheets[0]['cells'][$i][12],'in',12));
		$aboutus = trimhtml(dataIO($data->sheets[0]['cells'][$i][13],'in',2000));
		$bz = trimhtml(dataIO($data->sheets[0]['cells'][$i][14],'in',500));
		//数据对应转换
		if(!empty($mob)){
			if(ifmob($mob)){
				$row=$db->ROW(__TBL_USER__,"id","mob='$mob'");
				if($row){$s++;continue;}
			}else{
				$s++;continue;
			}
		}else{
			continue;
		}
		$nickname=dataIO($nickname,'in',50);
		if($sex=="女"){$sex=2;}else{$sex=1;}
		if(!ifdate($birthday)){$birthday='0000-00-00';}
		if(!empty($heigh)){$heigh=str_replace("cm","",$heigh);$heigh=intval($heigh);}else{$heigh=0;}
		if(!empty($weigh)){$weigh=str_replace("kg","",$weigh);$weigh=intval($weigh);}else{$weigh=0;}
		switch($love){case"未婚":$love=1;case"已婚":$love=2;break;case"丧偶":$love=4;break;case"离婚":$love=3;break;default:$love=0;break;}
		switch($edu){case"初中":$edu=1;break;case"高中":$edu=2;break;case"大专":$edu=3;break;case"本科":$edu=4;break;case"硕士":$edu=5;break;case"博士":$edu=6;break;default:$edu=0;break;}
		//地区处理
		if(!empty($areatitle)){
			$A=explode('/',$areatitle);
			$AT1 = $A[0];
			$AT2 = $A[1];
			$AT3 = $A[2];
			$AT4 = $A[3];
			if (!empty($AT1)){
				$rowa = $db->ROW(__TBL_AREA1__,"id","title LIKE '%".$AT1."%'","num");
				if ($rowa){
					$a1 = intval($rowa[0]);	
					$areaid = $a1;
					if (!empty($AT2)){
						$rowa = $db->ROW(__TBL_AREA2__,"id","fid=".$a1." AND title LIKE '%".$AT2."%'","num");
						if ($rowa){
							$a2 = intval($rowa[0]);
							$areaid = $a1.','.$a2;
							if (!empty($AT3)){
								$rowa = $db->ROW(__TBL_AREA3__,"id","fid=".$a2." AND title LIKE '%".$AT3."%'","num");
								if ($rowa){
									$a3 = $rowa[0];
									$areaid = $a1.','.$a2.','.$a3;
									if (!empty($AT4)){
										$rowa = $db->ROW(__TBL_AREA4__,"id","fid=".$a3." AND title LIKE '%".$AT4."%'","num");
										if ($rowa){
											$a4 = $rowa[0];
											$areaid = $a1.','.$a2.','.$a3.','.$a4;
										}
									}
								}
							}
						}
					}
				}
			}
		}
		$areatitle= str_replace("/"," ",$areatitle);
		$RZ='mob';
		$regtime = ADDTIME;
		$endtime = ADDTIME;
		$refresh_time= ADDTIME;
		$regkind=12;
		$dataflag=1;
		$flag=1;
		
		$admid  = $session_uid;
		$admname = $session_truename;
		$agentid = intval($session_agentid);
		$agenttitle = $session_agenttitle;
		//
		$db->query("INSERT INTO ".__TBL_USER__." (admid,admname,agentid,agenttitle,mob,sex,nickname,truename,birthday,heigh,weigh,love,areaid,areatitle,weixin,edu,RZ,aboutus,bz,regtime,endtime,refresh_time,dataflag,flag,regkind) VALUES ('$admid','$admname',$agentid,'$agenttitle','$mob','$sex','$nickname','$truename','$birthday','$heigh','$weigh','$love','$areaid','$areatitle','$weixin','$edu','$RZ','$aboutus','$bz','$regtime','$endtime','$refresh_time','$dataflag','$flag','$regkind')");
		$uid = $db->insert_id();
		//
		$SQL = '';
		$uname = 'excel'.$uid;
		$pwd = md5($mob);
		if(empty($nickname))$nickname='uid'.$uid;
		$db->query("UPDATE ".__TBL_USER__." SET uname='$uname',pwd='$pwd'".$SQL." WHERE id=".$uid);
		
		//V6
		set_data_ed_bfb($uid);
		
		//V7
		//shalou_init($uid);
		//set_data_ed_bfb($uid);
		$n++;
	}
	if($n>0)AddLog('【Excel导入用户】->'.$n.'个');
	alert('成功导入：'.$n.'个，失败：'.$s.'个','back');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<script src="<?php echo RES;?>www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<body>
<div class="navbox">
    <a href="u_add.php">人工录入</a>
	<a href="import.php" class="ed">Excel数据导入</a>
	<a href="import_weiyuan.php">微缘数据导入</a>
</div>
<div class="fixedblank"></div>
<form action="import.php?submitok=import" name="form_execl" method="post" enctype="multipart/form-data" onSubmit="zeai.msg('数据导入中，请匆关闭窗口...',{time:7200})">
<table class="table" style="margin-top:100px;">
    <tr>
    	<td width="500" align="center" class="border0" style="padding:20px 0;">
        <input type="file" accept="application/vnd.ms-excel" name="execl" class="input" style="width:420px;height:30px;line-height:30px;" /> <input type="submit" class="btn HUANG3" name="submit" value="开始导入" />
        </td>
    </tr>
    <tr><td align="left" style="font-size:14px;line-height:200%;padding:20px;background-color:#f5f5f5">注意事项：<br>
    1、导入的Execl表格请勿私自改动数据结构，请用官方标准模版填入数据进行导入　<a href="mb.xls" class="btn size2">下载Excel模版</a><br>
    2、必须填写手机号，没有手机或库中已存在手机号将跳过此条信息，在导入过程中请勿关闭页面<br>
    3、如果遇到导入出错，请联系管理员<br>
    4、由于数据量太大，请确认PHP环境超时时间足够长，一般至少要10分钟，发送过程中请不要关闭窗口，耐心等待<br>
    5、有手机的新密码为手机号码，导入后推荐用UID+密码方式登录<br>
	6、如果提醒没有选中的文件，请用微软Excel打开数据另存为 Excel97-2003格式，如下图<br>
    <img src="images/excel_1.png"></td></tr>
</table>
</form>
<?php require_once 'bottomadm.php';?>